package com.zengshuai.miniprogrambackend.Controller;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.zengshuai.miniprogrambackend.Common.constant.MyOss;
import com.zengshuai.miniprogrambackend.Common.constant.OssClient;
import com.zengshuai.miniprogrambackend.Entity.Comment;
import com.zengshuai.miniprogrambackend.Mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Comment")
public class CommentController {

    @Autowired
    MyOss oss;
    @Autowired
    OssClient ossClient;

    @Autowired
    CommentMapper commentMapper;

    @GetMapping("/QeuryCommentbyserviceid/{serviceid}")
    public List<Comment> QeuryCommentbyserviceid(@PathVariable("serviceid") String serviceid) {

        System.out.println(commentMapper.queryComment(serviceid));
        return commentMapper.queryComment(serviceid);
    }


    @GetMapping("/saveComment")
    public void saveComment(String comment, String rate, String nickname, String orderid, String openid, String serviceid, String headimageurl) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String currentTime = simpleDateFormat.format(new Date());


        commentMapper.saveComment(comment, rate, nickname, orderid, openid, serviceid, headimageurl, currentTime);


    }


    //保存评论图片到阿里云
    @PostMapping("/uploadCommentImage")
    public void uploadCommentImage(MultipartFile img, String openid, int i) throws IOException {

        String index = Integer.toString(i);
        System.out.println(index);
        System.out.println(openid);
        System.out.println(img);
        //创建oss对象
        OSS ossClient = this.ossClient.getOssClient();

        //获取上传的文件

        if (!img.isEmpty()) {
            //获取文件类型及后缀
            String[] fileType = img.getContentType().split("\\/");
            //文件属于image类型才允许执行下面操作
            if (fileType[0].equals("image")) {
                //获取文件后缀
                String suffix = "." + fileType[1];
                //设置存储的文件名
//
                String imgFileName = new Date().getTime() + suffix;
                try {

//                      oss存放路径
                    String ossimgObject = "妙邻维修" + "/" + "评论图片" + "/" + openid + "/" + imgFileName;
//                      将图片存入oss
                    ossClient.putObject(oss.getBUCKET_NAME(), ossimgObject, img.getInputStream());
                    //设置oss访问权限为公开读
                    ossClient.setObjectAcl(oss.getBUCKET_NAME(), ossimgObject, CannedAccessControlList.PublicRead);

                    //获取oss公开读路径
                    String imgUrl = "https://" + oss.getBUCKET_IP() + "/" + "妙邻维修" + "/" + "评论图片" + "/" + openid + "/" + imgFileName;

                    //将url地址存入数据库
                    commentMapper.saveImageurl(imgUrl, "commentimageurl" + index);


                    //捕获异常
                } catch (ClientException | OSSException ce) {
                    ce.printStackTrace();
                } finally {
                    this.ossClient.getOssClient().shutdown();
                }
            } else {

            }
        } else {

        }

    }


}
